var tenAi = function() {
    this.arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
    ai.call(this);
};
tenAi.prototype = Object.create(ai.prototype);

// 记录到受影响的数字
tenAi.prototype.insertToNumber = function(list) {
    // 记录到各受影响的号码
    var self = this;
    var result = [];
    for (var i = 0; i < 8; i++) {
        result[i] = {
            index: i + 1,
            total: 0,
            bet: [],
        };
    }
    for (var i in list) {
        var k = list[i].key;
        if (k.startsWith("和")) {
            for (var j = 0; j < 8; j++) {
                result[j].bet.push(k);
                result[j].total += list[i].amount;
            }
        }
        else {
            var k1 = k[0];
            var index = self.arr.indexOf(k1);
            if (index >= 0) {
                result[index].bet.push(k);
                result[index].total += list[i].amount;

                if (k[1] == '龙' || k[1] == '虎') {
                    var index2 = 7 - index;
                    result[index2].bet.push(k);
                    result[index2].total += list[i].amount;
                }
            }
        }
    }
    result.sort(function(a, b) {
        return b.total - a.total;
    });
    return result;
}

// 检测某个投注项，并应用概率
tenAi.prototype.apply = function(key, key2, arr1, arr2, all) {
    var self = this;
    if (key != key2) return null;
    if (this.checkRatio(self.rate)) {
        all = self.prior(all, arr1);
    }
    else {
        all = self.prior(all, arr2);
    }
    return all;
}

// 应用总和的概率
tenAi.prototype.he = function(result, index, he, he2) {
    var self = this;
    for (var i in result) {
        var data = result[i];
        self.deleteItem(data.bet, he);

        if (he2 == '和大' || he2 == '和小') {
            if (!data.pre) data.pre = [he2];
            else data.pre.push(he2);
        }
    }
    if (he2 == '和单' || he2 == '和双' || he2 == '和尾大' || he2 == '和尾小') {
        var data = result[7];
        if (!data.pre) data.pre = [he2];
        else data.pre.push(he2);
    }

    var arr = [];
    if (he2 == '和大') {
        for (var i = 8; i <= 20; i++) arr.push(i);
    }
    else if (he2 == '和小') {
        for (var i = 1; i <= 12; i++) arr.push(i);
    }
    else {
        for (var i = 1; i <= 20; i++) arr.push(i);
    }
    return arr;
}

// 取得总和的开奖数据
tenAi.prototype.heNumbers = function(index, he, ns) {
    var total = 0;
    var arr = [];
    var unopen = 0;
    for (var i = 0; i < 8; i++) {
        if (ns[i] === undefined) {
            unopen++;
            continue;
        }
        total += ns[i];
    }

    if (he == '和单' && total % 2 == 0 || he == '和双' && total % 2 == 1) {
        for (var i = 1; i <= 20; i += 2) {
            arr.push(i);
        }
        return arr;
    }
    if (he == '和单' && total % 2 == 1 || he == '和双' && total % 2 == 0) {
        for (var i = 2; i <= 20; i += 2) {
            arr.push(i);
        }
        return arr;
    }
    if (he == '和尾大') {
        for (var i = 1; i <= 20; i++) {
            if ((total + i) % 10 >= 5) arr.push(i);
        }
        return arr;
    }
    if (he == '和尾小') {
        for (var i = 1; i <= 20; i++) {
            if ((total + i) % 10 <= 4) arr.push(i);
        }
        return arr;
    }

    if (unopen > 1) {
        if (he == '和大') {
            for (var i = 7; i <= 20; i++) arr.push(i);
        }
        else {
            for (var i = 1; i <= 12; i++) arr.push(i);
        }
    }
    else {
        for (var i = 1; i <= 20; i++) {
            if (he == '和大' && total + i > 84) arr.push(i);
            else if (he == '和小' && total + i < 84) arr.push(i);
        }
    }
    return arr;
}

// 应用龙虎的概率，需要将信息记录到另外个关联号码
// lh - 用户的投注  lh2 - 实际开奖结果
tenAi.prototype.longhu = function(result, index, lh, lh2) {
    var self = this;
    var index2 = 9 - index;
    for (var i in result) {
        if (result[i].index != index2) continue;
        var data = result[i];

        self.deleteItem(data.bet, lh);
        if (!data.pre) data.pre = [lh2];
        else data.pre.push(lh2);
    }
    if (lh2[1] == '龙') {
        return self.range(4, 20);
    }
    else if (lh2[1] == '虎') {
        return self.range(1, 17);
    }
    return self.range(1, 20);
}

// 取得龙虎和的开奖数据
tenAi.prototype.longhuNumbers = function(index, lh, ns) {
    var index2 = 9 - index;
    var n = ns[index2 - 1];

    var arr = [];
    if (lh[1] == '龙') {
        for (var i = 1; i < n; i++) arr.push(i);
    }
    else {
        for (var i = n + 1; i <= 20; i++) arr.push(i);
    }
    return arr;
}

// 入口
tenAi.prototype.open = function(bets, lottery, rate) {
    var self = this;
    self.map = lottery;
    self.rate = rate;
    
    // 合并下数据
    var arr = [["和大", "和小"], ["和单", "和双"], ["和尾大", "和尾小"]];
    var arr2 = self.arr;
    for (var i in arr2) {
        var k = arr2[i];
        arr.push([k + "大", k + "小"]);
        arr.push([k + "单", k + "双"]);
        arr.push([k + "尾大", k + "尾小"]);
        arr.push([k + "合单", k + "合双"]);
        arr.push([k + "龙", k + "虎"]);
    }
    var list = self.mixBets(bets, arr);

    // 记录到各受影响的号码
    var result = self.insertToNumber(list);

    // 逐个号码开出来
    var numbers = self.range(1, 20);
    var lottery = [];
    var ns = [];
    for (var i in result) {
        var index = result[i].index;
        var bets = result[i].bet;
        if (self.isEmpty(bets) && self.isEmpty(result[i].pre)) continue;

        var all = numbers.concat([]);
        if (result[i].pre) {
            // 这些受其他号码影响，需要优先处理
            for (var j in result[i].pre) {
                var k = result[i].pre[j];
                if (k[1] == '龙' || k[1] == '虎') {
                    all = self.prior(all, self.longhuNumbers(index, k, ns));
                }
                else if (k.startsWith("和") || k == '尾大' || k == '尾小') {
                    all = self.prior(all, self.heNumbers(index, k, ns));
                }
            }
        }
        for (var j in bets) {
            var k = bets[j];
            if (!k) continue;
            var k2 = k.substring(1);
            do {
                if (k2 == '龙' || k2 == '虎') {
                    if (self.checkRatio(rate)) {
                        all = self.prior(all, self.longhu(result, index, k, k[0] + (k2 == '龙' ? '虎' : '龙')));
                    }
                    else {
                        all = self.prior(all, self.longhu(result, index, k, k));
                    }
                    break;
                }
                else if (k == '和大') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和小'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和小') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和大'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和单') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和双'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和双') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和单'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和尾大') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和尾小'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }
                else if (k == '和尾小') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和尾大'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                    break;
                }

                if (k2 == '大') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
                    else 
                        all = self.prior(all, [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]);
                    break;
                }
                if (k2 == '小') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]);
                    else 
                        all = self.prior(all, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
                    break;
                }
                if (k2 == '单') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]);
                    else 
                        all = self.prior(all, [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]);
                    break;
                }
                if (k2 == '双') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]);
                    else 
                        all = self.prior(all, [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]);
                    break;
                }
                if (k2 == '尾大') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, [1, 2, 3, 4, 10, 11, 12, 13, 14, 20]);
                    else 
                        all = self.prior(all, [5, 6, 7, 8, 9, 15, 16, 17, 18, 19]);
                    break;
                }
                if (k2 == '尾小') {
                    if (self.checkRatio(rate))
                        all = self.prior(all, [5, 6, 7, 8, 9, 15, 16, 17, 18, 19]);
                    else 
                        all = self.prior(all, [1, 2, 3, 4, 10, 11, 12, 13, 14, 20]);
                    break;
                }
                if (k2 == '合单') {
                    if (self.checkRatio(rate))
                        all = self.prior(all, [2, 4, 6, 8, 11, 13, 15, 17, 19, 20]);
                    else 
                        all = self.prior(all, [1, 3, 5, 7, 9, 10, 12, 14, 16, 18]);
                    break;
                }
                if (k2 == '合双') {
                    if (self.checkRatio(rate))
                        all = self.prior(all, [1, 3, 5, 7, 9, 10, 12, 14, 16, 18]);
                    else 
                        all = self.prior(all, [2, 4, 6, 8, 11, 13, 15, 17, 19, 20]);
                    break;
                }
                if (k2 == '中') {
                    if (self.checkRatio(1 - (1 - rate)/(1 - rate + 2 * rate))) 
                        all = self.prior(all, self.range(8, 20));
                    else 
                        all = self.prior(all, self.range(1, 7));
                    break;
                }
                if (k2 == '发') {
                    if (self.checkRatio(1 - (1 - rate)/(1 - rate + 2 * rate))) 
                        all = self.prior(all, [1, 2, 3, 4, 5, 6, 15, 16, 17, 18, 19, 20]);
                    else 
                        all = self.prior(all, self.range(8, 14));
                    break;
                }
                if (k2 == '白') {
                    if (self.checkRatio(1 - (1 - rate)/(1 - rate + 2 * rate))) 
                        all = self.prior(all, self.range(1, 14));
                    else 
                        all = self.prior(all, self.range(15, 20));
                    break;
                }
                if (k2 == '东') {
                    if (self.checkRatio(1 - (1 - rate)/(1 - rate + 3 * rate))) 
                        all = self.prior(all, [2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15, 16, 18, 19, 20]);
                    else 
                        all = self.prior(all, [1, 5, 9, 13, 17]);
                    break;
                }
                if (k2 == '南') {
                    if (self.checkRatio(1 - (1 - rate)/(1 - rate + 3 * rate))) 
                        all = self.prior(all, [1, 3, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 17, 19, 20]);
                    else 
                        all = self.prior(all, [2, 6, 10, 14, 18]);
                    break;
                }
                if (k2 == '西') {
                    if (self.checkRatio(1 - (1 - rate)/(1 - rate + 3 * rate))) 
                        all = self.prior(all, [1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, 20]);
                    else 
                        all = self.prior(all, [3, 7, 11, 15, 19]);
                    break;
                }
                if (k2 == '北') {
                    if (self.checkRatio(1 - (1 - rate)/(1 - rate + 3 * rate))) 
                        all = self.prior(all, [1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19]);
                    else 
                        all = self.prior(all, [4, 8, 12, 16, 20]);
                    break;
                }
            } while (false);
            if (all.length == 1) break;
        }

        // 应用概率，抽取一个出来
        ns[index - 1] = self.random(all);
        self.deleteItem(numbers, ns[index - 1]);
    }

    // 还没有开出的号码，随机开了
    for (var i = 0; i < 8; i++) {
        if (ns[i] !== undefined) continue;

        ns[i] = self.random(numbers);
        self.deleteItem(numbers, ns[i]);
    }

    // 返回开奖结果
    return ns;
}
var TenAi = new tenAi();
